﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MTC.Common;
using MTC.Common.Enum;
using MTC.Web.Business;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class PlatesEdit : System.Web.UI.Page
    {
        private PlateEntity _entity;
        Business.Plates PlatesBO = new Business.Plates();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the plate id if available
                if (Request.Params[Params.PLATEID] != "0")
                {
                    int plateid = Convert.ToInt32(Request.Params[Params.PLATEID]);
                    _entity = PlatesBO.GetAPlate(plateid);
                    LoadFields();
                }
            }

        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfPlateId.Value = _entity.PlateId.ToString();
            cboPlateType.SelectedIndex = _entity.PlateTypeId;
            txtPlateNumber.Text = _entity.PlateNumber;

            cboOwner.SelectedValue = _entity.OwnerId.ToString();
            if (_entity.Financial.WeeklyRate != null)
                txtWeekRate.Text = _entity.Financial.WeeklyRate;
            if (_entity.Financial.MonthlyRate != null)
                txtMonthRate.Text = _entity.Financial.MonthlyRate;
            if (_entity.Financial.StartDate.HasValue)
                txtStartDate.Text = Utils.ChangeFormat(_entity.Financial.StartDate.Value);
            if (_entity.Financial.EndDate.HasValue)
                txtEndDate.Text = Utils.ChangeFormat(_entity.Financial.EndDate.Value);
            if (_entity.Financial.Bank != null)
                txtBank.Text = _entity.Financial.Bank;
            if (_entity.Financial.Account != null)
                txtAccount.Text = _entity.Financial.Account;
            if (_entity.Financial.Comment != null)
                txtComment.Text = _entity.Financial.Comment;


        }

        private void SetupEnvironment()
        {
            cboPlateType.DataSource = PlateType.GetPlateTypes();
            cboPlateType.DataBind();
            cboOwner.DataSource = Web.Business.DropDowns.GetOwners();
            cboOwner.DataBind();

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                SetFields();
            }

        }

        private bool SetFields()
        {
            PlateEntity entity = new PlateEntity();
            PlateFinancialEntity financialEntity = new PlateFinancialEntity();

            entity.Financial = financialEntity;

            if (hfPlateId.Value != "")
                entity.PlateId = Convert.ToInt32(hfPlateId.Value);
            if (txtPlateNumber.Text.Length > 0)
                entity.PlateNumber = txtPlateNumber.Text;
            entity.PlateTypeId = cboPlateType.SelectedIndex + 1;
            entity.Financial.PlateId = entity.PlateId;
            entity.OwnerId = Convert.ToInt32(cboOwner.SelectedItem.Value);
            
            if (txtAccount.Text.Length > 0)
                entity.Financial.Account = txtAccount.Text;
            if (txtBank.Text.Length > 0)
                entity.Financial.Bank = txtBank.Text;
            if (txtStartDate.Text.Length > 0)
                entity.Financial.StartDate = Convert.ToDateTime(txtStartDate.Text);
            if (txtEndDate.Text.Length > 0)
                entity.Financial.EndDate = Convert.ToDateTime(txtEndDate.Text);
            if (txtComment.Text.Length > 0)
                entity.Financial.Comment = txtComment.Text;
            if (txtMonthRate.Text.Length > 0)
                entity.Financial.MonthlyRate = txtMonthRate.Text;
            if (txtWeekRate.Text.Length > 0)
                entity.Financial.WeeklyRate = txtWeekRate.Text;


            if (entity.PlateId == 0)
            {
                PlatesBO.AddPlate(entity);
            }
            else
            {
                return PlatesBO.UpdateAPlate(entity);
            }
            return true;
            
        }

        private bool ValidateFields()
        {
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.PlateListURL(), true);

        }

        private void ClearFields()
        {
            txtPlateNumber.Text = "";
            cboPlateType.ClearSelection();
            cboOwner.ClearSelection();
            txtAccount.Text = "";
            txtBank.Text = "";
            txtComment.Text = "";
            txtEndDate.Text = "";
            txtStartDate.Text = "";
            txtMonthRate.Text = "";
            txtWeekRate.Text = "";

        }

        protected void ASPxPageControl1_ActiveTabChanged(object source, DevExpress.Web.ASPxTabControl.TabControlEventArgs e)
        {

        }
    }
}
